/*
 * Copyright (C) 2012 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.gui;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import ch.bfh.aeneas.R;
import ch.bfh.aeneas.navigation.RouteInfo;
import java.util.List;

/**
 *
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class RouteInfoListAdapter extends ArrayAdapter<RouteInfo> {
    
    private Context mCtx;
    private int mResId;
    private RouteInfo[] mData;
    
    public RouteInfoListAdapter(Context ctx, int resViewId, List<RouteInfo> objects) {
        super(ctx, resViewId, objects);
        
        mCtx = ctx;
        mResId = resViewId;
        mData = (RouteInfo[]) objects.toArray();
    }
    
    public RouteInfoListAdapter(Context ctx, int resViewId, RouteInfo[] objects) {
        super(ctx, resViewId, objects);
        
        mCtx = ctx;
        mResId = resViewId;
        mData = objects;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RouteInfo r;
        RouteHolder rh;
        
        if(row == null) {        
            LayoutInflater inflater = ((Activity) mCtx).getLayoutInflater();
            row = inflater.inflate(mResId, parent, false);
            
            rh = new RouteHolder();
            rh.txtTitle = (TextView) row.findViewById(R.id.txtRowTitle);
            rh.txtLength = (TextView) row.findViewById(R.id.txtLength);
            rh.txtDistance = (TextView) row.findViewById(R.id.txtDistance);
            rh.txtNearness = (TextView) row.findViewById(R.id.txtNearness);
            
            row.setTag(rh);            
        } else {            
            rh = (RouteHolder) row.getTag();
        }

        r = mData[position];
        rh.txtTitle.setText(r.getName());
        rh.txtLength.setText(r.getLength()+"");
        rh.txtDistance.setText(r.getDistanceToStart()+"");
        rh.txtNearness.setText(r.getDistanceToTarget()+"");
            
        return row;
    }

    private class RouteHolder {
        
        TextView txtTitle;
        TextView txtLength;
        TextView txtDistance;
        TextView txtNearness;
        
    }
}
